package io.bloom.web.model;

import io.bloom.search.Searchable;
import lombok.Data;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.StoredField;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Data
public class LinkDO implements Searchable {

    private long id;
    private String title;
    private String body;

    public LinkDO() {
    }

    public LinkDO(long id, String t, String b) {
        this.id = id;
        this.title = t;
        this.body = b;
    }

    @Override
    public int compareTo(Searchable o) {
        return (int) ((int) this.id() - o.id());
    }

    @Override
    public long id() {
        return id;
    }

    @Override
    public Map<String, String> storeDocument() {
        return new HashMap<String, String>() {{
            put("title", title);
            put("body", body);
        }};
    }

    @Override
    public Map<String, String> indexDocument() {
        return new HashMap<String, String>() {{
            put("title", title);
            put("id", String.valueOf(id));
        }};
    }

    @Override
    public List<? extends Searchable> listAfter(long id, int count) {
        return Collections.emptyList();
    }

    @Override
    public LinkDO fromDocument(Document document) {
        title = document.get("title");
        body = document.get("body");

        id = NumberUtils.toLong(document.get(DOC_ID), 0);
        return this;
    }

    @Override
    public Document toDocument() {

        Document doc = new Document();
        //index
        doc.add(new StoredField(DOC_ID, id));
        doc.add(new StoredField(DOC_CLASSNAME, getClass().getName()));
        doc.add(new StoredField("title", title));
        doc.add(new StoredField("body", body));

        return doc;
    }


    @Override
    public float boost() {
        return 1.0f;
    }
}
